package net.yunzhanyi.client.controller;

import net.yunzhanyi.client.annotation.CheckLogin;
import net.yunzhanyi.client.annotation.CheckWebLogin;
import net.yunzhanyi.client.domain.dto.ClientLoginUser;
import net.yunzhanyi.client.domain.dto.CollectionDto;
import net.yunzhanyi.client.domain.vo.PageVo;
import net.yunzhanyi.client.service.CollectionService;
import net.yunzhanyi.client.utils.AuthUtils;
import net.yunzhanyi.common.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author bestct
 * @date 2022/8/19
 * @type 类
 */
@Controller
public class CollectionController {

    @Autowired
    private CollectionService collectionService;


    @CheckWebLogin
    @GetMapping("/collection")
    public String collection(
            @RequestParam(value = "collectionType", defaultValue = "0") Integer collectionType,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            Model model) {

        ClientLoginUser loginUser = AuthUtils.getLoginUser();
        PageVo<CollectionDto> collectionDtoPageVo = collectionService.getUserCollection(loginUser.getUid(), collectionType, pageNum, pageSize);
        model.addAttribute("dataInfo", collectionDtoPageVo);
        model.addAttribute("collectionType", collectionType);
        model.addAttribute("userSilde", "collection");
        return "collection";
    }

    @CheckLogin
    @PostMapping("/api/collection")
    @ResponseBody
    public AjaxResult addCollection(@RequestParam Long resId,
                                    @RequestParam(defaultValue = "1") Integer collectionType) {
        System.out.println(collectionType);
        collectionService.addCollection(resId, AuthUtils.getUid(), collectionType);
        return AjaxResult.success();
    }

    @CheckLogin
    @DeleteMapping("/api/collection")
    @ResponseBody
    public AjaxResult cancelCollection(@RequestParam Long resId,
                                       @RequestParam(defaultValue = "1") Integer collectionType) {
        collectionService.cancelCollection(resId, AuthUtils.getUid(), collectionType);
        return AjaxResult.success();
    }
    @CheckLogin
    @DeleteMapping("/api/collection/{colId}")
    @ResponseBody
    public AjaxResult cancelCollection(@PathVariable Long colId) {
        collectionService.cancelCollectionById(colId);
        return AjaxResult.success();
    }

    @CheckLogin
    @PostMapping("/api/collection/{colId}")
    @ResponseBody
    public AjaxResult reCollection(@PathVariable Long colId) {
        collectionService.reCollectionById(colId);
        return AjaxResult.success();
    }


}
